/*
 * @文件描述:板载LED驱动
 * @版本:
 * @作者: 周晨阳
 * @Date: 2021-01-16 17:00:58
 */

#include "LED.h"
#ifndef WIN_SIM
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define BOARD_LED_RED_PIN GPIO_Pin_9
#define BOARD_LED_GREEN_PIN GPIO_Pin_10
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* Public functions ---------------------------------------------------------*/
/**
 * @brief : 板载LED初始化
 * @param {*}
 * @return {*}
 */
void LED_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //使能GPIOF时钟

  // GPIOF9,F10初始化设置
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // LED0和LED1对应IO口
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;           //普通输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;          //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;      // 100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;            //上拉
  GPIO_Init(GPIOF, &GPIO_InitStructure);                  //初始化GPIO

  GPIO_SetBits(GPIOF, GPIO_Pin_9 | GPIO_Pin_10); // GPIOF9,F10设置高，灯灭
  printf("LED init done...\n");
}
/**
 * @brief : 开启对应LED
 * @param {uint8_t} led
 * @return {*}
 */
void openLed(uint8_t led)
{
  if (led == LED_RED)
    GPIO_ResetBits(GPIOF, BOARD_LED_RED_PIN); // 灯灭
  else
  {
    GPIO_ResetBits(GPIOF, BOARD_LED_GREEN_PIN); // 灯灭
  }
}
/**
 * @brief : 关闭对应LED
 * @param {uint8_t} led
 * @return {*}
 */
void closeLed(uint8_t led)
{
  if (led == LED_RED)
    GPIO_SetBits(GPIOF, BOARD_LED_RED_PIN); // 灯灭
  else
  {
    GPIO_SetBits(GPIOF, BOARD_LED_GREEN_PIN); // 灯灭
  }
}
/**
 * @brief : 反转LED状态
 * @param {uint8_t} led
 * @return {*}
 */
void trigLed(uint8_t led)
{
  if (led == LED_RED)
  {
    if (GPIO_ReadOutputDataBit(GPIOF, BOARD_LED_RED_PIN))
    {
      GPIO_ResetBits(GPIOF, BOARD_LED_RED_PIN);
    }
    else
    {
      GPIO_SetBits(GPIOF, BOARD_LED_RED_PIN);
    }
  }
  else
  {
    if (GPIO_ReadOutputDataBit(GPIOF, BOARD_LED_GREEN_PIN))
    {
      GPIO_ResetBits(GPIOF, BOARD_LED_GREEN_PIN);
    }
    else
    {
      GPIO_SetBits(GPIOF, BOARD_LED_GREEN_PIN);
    }
  }
}
#endif
